import path from 'path'
import ts from 'rollup-plugin-typescript2'
import resolve from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import babel from 'rollup-plugin-babel'
import commonjs from '@rollup/plugin-commonjs'
import json from 'rollup-plugin-json'
import css from 'rollup-plugin-css-porter'
import postcss from 'rollup-plugin-postcss'
import image from '@rollup/plugin-image';
import { terser } from 'rollup-plugin-terser';
import simplevars from 'postcss-simple-vars';
// 处理less嵌套样式写法
import nested from 'postcss-nested';
// 可以提前适用最新css特性（已废弃由postcss-preset-env替代，但还是引用进来了。。。）
// import cssnext from 'postcss-cssnext';
// 替代cssnext
import postcssPresetEnv from 'postcss-preset-env';
// css代码压缩
import cssnano from 'cssnano';
import serve from 'rollup-plugin-serve'
import { eslint } from 'rollup-plugin-eslint'

const env = process.env.NODE_ENV;

export default {
    input: "src/index.ts",
    output: [{
        file: path.resolve(`dist/index${env === 'production' ? '.min' : ''}.js`),
        format: 'umd',
        name: 'geneUI',
        sourcemap: true //生成映射文件
    }],
    onwarn: function (warning) {
        if (warning.code === 'THIS_IS_UNDEFINED') {
            return;
        }
    },
    external: ['antd', '@ant-design/icons', 'react', 'prop-types', 'gojs'],
    plugins: [
        image(),
        postcss({
            plugins: [
                simplevars(),
                nested(),
                // cssnext({ warnForDuplicates: false, }),
                postcssPresetEnv(),
                cssnano(),
            ],
            // 处理.css和.less文件
            extensions: ['.css', 'less'],
        }),
        resolve({
            extensions: [".js", ".ts", ".tsx"],
        }),
        babel({
            exclude: '**/node_modules/**',
            runtimeHelpers: true,
            plugins: [
                "@babel/plugin-external-helpers"
            ]
        }),
        ts({
            tsconfig: path.resolve(__dirname, "tsconfig.json")
        }),
        replace({
            // exclude: 'node_modules/**',
            'process.env.NODE_ENV': JSON.stringify(env || 'development'),
        }),
        commonjs({
            'namedExports': {
                'node_modules/react-is/index.js': ['isFragment'],
                'node_modules/react/index.js': ['Fragment', 'cloneElement', 'isValidElement', 'Children', 'createContext', 'Component', 'useRef', 'useImperativeHandle', 'forwardRef', 'useState', 'useEffect', 'useMemo'],
                'node_modules/react-dom/index.js': ['render', 'unmountComponentAtNode', 'findDOMNode'],
                'node_modules/gojs/release/go.js': ['Diagram', 'GraphLinksModel', 'Overview', 'Spot']
            }
        }),
        json({
            include: "node_modules/**",
            compact: true,
            namedExports: true,
        }),
        // 生产环境执行terser压缩代码
        (env === 'production' && terser()),
    ]
}